package cn.lopr.im.android.mvp.view.adapters;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.util.List;

import cn.lopr.im.android.R;
import cn.lopr.im.android.bean.User;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * @author shuangyang7
 * @date 2021/1/1 20:29
 */
public class SortRecyclerViewAdapter extends RecyclerView.Adapter<SortRecyclerViewAdapter.ViewHolder>
        implements SectionIndexer, View.OnClickListener, View.OnLongClickListener {

    private List<User> mValues;
    private final Context context;

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public SortRecyclerViewAdapter(Context context, List<User> items) {
        this.mValues = items;
        this.context = context;
    }

    public void add(List<User> users) {
        this.mValues = users;
        this.notifyDataSetChanged();
        this.notifyItemRangeChanged(0, this.mValues.size());
    }

    @NonNull
    @Override
    public SortRecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_contact, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SortRecyclerViewAdapter.ViewHolder holder, int position) {
        holder.mItem = this.mValues.get(position);
        holder.itemView.setTag(position);
        holder.llContact.setTag(position);

        if (mOnItemClickListener != null) {
            //holder.itemView.setOnClickListener(view -> mOnItemClickListener.onItemClick(holder.itemView, position));
            holder.llContact.setOnClickListener(view -> mOnItemClickListener.onItemClick(holder.itemView, position));
        }
        //根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);

        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if (position == getPositionForSection(section)) {
            holder.tvCatalog.setVisibility(View.VISIBLE);
            holder.tvCatalog.setText(holder.mItem.getSortLetters());

        } else {
            holder.tvCatalog.setVisibility(View.GONE);
            holder.lvCatalog.setVisibility(View.GONE);
        }

        holder.tvNickname.setText(holder.mItem.getNickname());
        holder.tvIntroduction.setText(holder.mItem.getIntroduction());

        Glide.with(context).load(holder.mItem.getPortrait())
                .placeholder(R.drawable.default_head_portrait)
                .dontAnimate()
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        holder.ivPortrait.setImageDrawable(resource);
                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                        holder.ivPortrait.setImageDrawable(errorDrawable);
                    }
                });
    }

    @Override
    public int getItemCount() {
        return this.mValues.size();
    }

    @Override
    public Object[] getSections() {
        return new Object[0];
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    @Override
    public int getSectionForPosition(int position) {
        return mValues.get(position).getSortLetters().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    @Override
    public int getPositionForSection(int section) {
        for (int i = 0; i < mValues.size(); i++) {
            String sortStr = mValues.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {      //getTag获取的即是点击位置
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemLongClick(v, (int) v.getTag());
        }
        return true;
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int section);

        void onItemLongClick(View view, int section);
    }

    public void onRecyclerViewItemClickListener(OnRecyclerViewItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View mView;

        public TextView tvCatalog;
        public CircleImageView ivPortrait;
        public TextView tvNickname;
        public TextView tvIntroduction;
        public LinearLayout llContact;
        public LinearLayout lvCatalog;

        public User mItem;

        public ViewHolder(View view) {
            super(view);
            this.mView = view;
            this.lvCatalog = view.findViewById(R.id.lv_catalog);
            this.tvCatalog = view.findViewById(R.id.tv_catalog);
            this.ivPortrait = view.findViewById(R.id.iv_portrait);
            this.tvNickname = view.findViewById(R.id.tv_nickname);
            this.tvIntroduction = view.findViewById(R.id.tv_introduction);
            this.llContact = view.findViewById(R.id.ll_contact);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + tvNickname.getText() + "'";
        }
    }


}
